package com.lanou3g.musicenjoy.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.activities.LoginActivity;
import com.lanou3g.musicenjoy.eventbus.MyEvent;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by 李宁 on 15/10/23.
 * 我的页面
 */
public class MineFragment extends Fragment implements View.OnClickListener {


    private LinearLayout mine_login;
    private LinearLayout mine_local;
    private LinearLayout mine_song_download;
    private LinearLayout mine_mv_download;
    private LinearLayout mine_recently_play;
    private LinearLayout mine_myfavorite;
    private LinearLayout mine_collectsong;
    private NativeFragment nativeFragment;
    private CircleImageView mine_img;//头像
    private TextView mine_logintext;//昵称
    private TextView song_num,mine_myfavorite_num;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_mine, null);
        Log.i("lanou", "------42");

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mine_login = (LinearLayout) getActivity().findViewById(R.id.mine_login);
        mine_local = (LinearLayout) getActivity().findViewById(R.id.mine_local);
        mine_song_download = (LinearLayout) getActivity().findViewById(R.id.mine_song_download);
        mine_mv_download = (LinearLayout) getActivity().findViewById(R.id.mine_mv_download);
        mine_recently_play = (LinearLayout) getActivity().findViewById(R.id.mine_recently_play);
        mine_myfavorite = (LinearLayout) getActivity().findViewById(R.id.mine_myfavorite);
        mine_collectsong = (LinearLayout) getActivity().findViewById(R.id.mine_collectsong);
        mine_img = (CircleImageView) getActivity().findViewById(R.id.mine_img);
        mine_logintext = (TextView) getActivity().findViewById(R.id.mine_logintext);
        song_num = (TextView) getActivity().findViewById(R.id.song_num);
        mine_myfavorite_num = (TextView) getActivity().findViewById(R.id.mine_myfavorite_num);
        mine_login.setOnClickListener(this);
        mine_local.setOnClickListener(this);
        mine_collectsong.setOnClickListener(this);
        mine_mv_download.setOnClickListener(this);
        mine_myfavorite.setOnClickListener(this);
        mine_song_download.setOnClickListener(this);
        mine_recently_play.setOnClickListener(this);
        mine_logintext.setOnClickListener(this);

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("xiaoxin", Context.MODE_PRIVATE);
        SharedPreferences sharedPreferencesnum = getActivity().getSharedPreferences("lining",Context.MODE_PRIVATE);//我的最爱首
        song_num.setText(sharedPreferences.getInt("count", 0) + "");
        mine_myfavorite_num.setText(sharedPreferencesnum.getInt("countnum", 0) + "");//我的最爱首
      //  EventBus.getDefault().register(this);



    }

    //接收QQ数据
    public void onEvent(MyEvent.Landing event) {
        mine_logintext.setText(event.getData().get("screen_name").toString());
        Picasso.with(getActivity()).load(event.getData().get("profile_image_url").toString()).into(mine_img);

    }
    //接收微博数据
    public void onEvent(MyEvent.weiboLanding event){
        mine_logintext.setText(event.getInfo().get("screen_name").toString());
        Picasso.with(getActivity()).load(event.getInfo().get("profile_image_url").toString()).into(mine_img);

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //歌曲下载
            case R.id.mine_song_download:
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, new DownloadContainFragment())
                        .commit();
                break;
            //收藏的歌单
            case R.id.mine_collectsong:
                break;
            //我的最爱
            case R.id.mine_myfavorite:
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, new MyFavoriteFragment())
                        .commit();
                break;
                //登录
            case R.id.mine_login:
                if (mine_logintext.getText().equals("登录/注册")){
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(intent, 67);
                getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                }
                break;
            //本地
            case R.id.mine_local:
                nativeFragment=new NativeFragment();
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container,nativeFragment)
                        .commit();
                break;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

